//
//  HeadIconView.m
//  Rongcloud
//
//  Created by LiuLinhong on 2018/03/01.
//  Copyright © 2018年 Beijing Rongcloud Network Technology Co. , Ltd. All rights reserved.
//

#import "HeadIconView.h"
#import "NSColor+ColorChange.h"
#import "CommonUtility.h"
#import <Quartz/Quartz.h>

#define RegexIsChinese @"^[\u4e00-\u9fa5]+$"

@interface HeadIconView ()
{
    NSTextField *userNameTextField;
}

@end

@implementation HeadIconView


- (instancetype)initWithFrame:(CGRect)frame withUserName:(NSString *)userName withColor:(NSColor *)color
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.userName = userName;
//        self.userType = userType;
        self.backgroundColor = color;
        self.wantsLayer = YES;
        self.layer.cornerRadius = 2.0;
        
 
        
        if (CGRectEqualToRect(frame, RightTableHeaderFrame))
            [self initTableHeadView];
        else if (CGRectEqualToRect(frame, SwitchPageHeaderFrame))
            [self initSwitchHeadView];
    }
    
    return self;
}

- (void)initTableHeadView
{
    userNameTextField = [[NSTextField alloc] initWithFrame:CGRectMake(-4, self.frame.size.height / 4, self.frame.size.width+8, self.frame.size.height / 2)];
    userNameTextField.stringValue = [self processUserName];
    userNameTextField.bordered = NO;
    userNameTextField.editable = NO;
    userNameTextField.alignment = NSTextAlignmentCenter;
    userNameTextField.font = [NSFont systemFontOfSize:12.0];
    userNameTextField.textColor = [NSColor whiteColor];
    userNameTextField.backgroundColor = [NSColor clearColor];
    [self addSubview:userNameTextField];
}

- (void)initSwitchHeadView
{
    userNameTextField = [[NSTextField alloc] initWithFrame:CGRectMake(-4, (self.frame.size.height - self.frame.size.height / 4) / 2, self.frame.size.width+8, self.frame.size.height / 4)];
    userNameTextField.stringValue = [self processUserName];
    userNameTextField.bordered = NO;
    userNameTextField.editable = NO;
    userNameTextField.alignment = NSTextAlignmentCenter;
    userNameTextField.font = [NSFont systemFontOfSize:18.0];
    userNameTextField.textColor = [NSColor whiteColor];
    userNameTextField.backgroundColor = [NSColor clearColor];
    [self addSubview:userNameTextField];
    
    /*
    NSImageView *imageView = [[NSImageView alloc] initWithFrame:CGRectMake(40, 40, 80, 80)];
//    logoImageView.layer.cornerRadius = 5.0;
    imageView.image = [NSImage imageNamed:@"MeetingCloseCameraHeaderImage"];
    [self addSubview:imageView];
    
    self.wantsLayer = YES;
    imageView.wantsLayer = YES;
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.duration = 1.0;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    animation.fromValue = @0.0f;
    animation.toValue = [NSNumber numberWithFloat:-2 * M_PI];
    animation.repeatCount = NSUIntegerMax;
    animation.fillMode = kCAFillModeBackwards;
    
    imageView.layer.anchorPoint = CGPointMake(0.5, 0.5);
    [imageView.layer addAnimation:animation forKey:@"rotation"];
     */
}

- (NSString *)processUserName
{
    NSString *tmpUserName = [CommonUtility strimCharacter:self.userName withRegex:RegexIsChinese];
    if (!tmpUserName)
        return @"";
    else
        return tmpUserName;
}

@end
